package com.qf.administration.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.administration.entity.ConferenceManage;
import com.qf.administration.mapper.ConferenceManageMapper;
import com.qf.administration.qo.ManageQO;
import com.qf.administration.service.ManageService;
import com.qf.administration.utils.CommonBeanUtils;
import com.qf.administration.vo.ManageVo;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.RespResult;
import com.qf.common.base.result.ResultCode;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * @Title: ManageServiceImpl
 * @Description:
 * @author:ZWX
 * @date: 2023/5/20 17:02
 */
@Service
public class ManageServiceImpl implements ManageService {
    @Resource
    private ConferenceManageMapper conferenceManageMapper;

    //添加
    @Override
    public RespResult<String> addManage(ManageQO manageQo) {
        /*首先查询id是否存在*/
        ConferenceManage conferenceManage1 = conferenceManageMapper.selectById(manageQo.getId());
        if (ObjectUtils.isEmpty(conferenceManage1)) {
            /*id不存在，则可以进行添加*/
            ConferenceManage conferenceManage = new ConferenceManage();
            BeanUtils.copyProperties(manageQo, conferenceManage);
            int insert = conferenceManageMapper.insert(conferenceManage);
            if (insert > 0) {
                return RespResult.success("会议室添加成功");
            } else {
                throw new ServiceException(ResultCode.SYS_ERROR);
            }
        } else {
            /*id存在，添加失败*/
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }

    }

    //伪删除
    @Override
    public RespResult<String> deleteManage(int[] ids) {
        int count = conferenceManageMapper.deleteByMemberId(ids);
        if (count != ids.length) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        } else {
            return RespResult.success("删除成功");
        }
    }

    //修改
    @Override
    public RespResult<String> updateManage(ManageQO manageQo) {
        ConferenceManage conferenceManage = new ConferenceManage();
        BeanUtils.copyProperties(manageQo, conferenceManage);
        int i = conferenceManageMapper.updateById(conferenceManage);
        if (i < 1) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
        return RespResult.success("更新成功");
    }

    //查询
    @Override
    public PageInfo<ManageVo> queryManage(int page, int size, String meetingRoomName, String sortName) {
        PageInfo<ConferenceManage> pageInfo = PageHelper.startPage(page, size).doSelectPageInfo(() -> conferenceManageMapper.selectList(sortName, meetingRoomName));
        if (pageInfo.getTotal() == 0) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
        return CommonBeanUtils.copyPageInfo(pageInfo, new PageInfo<>(), ManageVo::new);
    }

    @Override
    public ManageVo queryManageOne(int id) {
        ConferenceManage conferenceManage = conferenceManageMapper.selectById(id);
        if (ObjectUtils.isEmpty(conferenceManage)) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        } else {
            ManageVo manageVo = new ManageVo();
            BeanUtils.copyProperties(conferenceManage, manageVo);
            return manageVo;
        }
    }
}


